package com.yourname.myapplication.activity

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.yourname.myapplication.database.AppDatabase
import com.yourname.myapplication.model.Question
import com.yourname.myapplication.ui.theme.MyApplicationTheme
import kotlinx.coroutines.launch

class QuestionActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                val category = intent.getStringExtra("category") ?: ""
                var questions by remember { mutableStateOf(listOf<Question>()) }

                LaunchedEffect(key1 = category) {
                    launch {
                        questions = AppDatabase.getInstance(applicationContext).questionDao().getQuestionsByCategory(category)
                    }
                }

                QuestionScreen(questions)
            }
        }
    }
}

@Composable
fun QuestionScreen(questions: List<Question>) {
    var currentQuestionIndex by remember { mutableStateOf(0) }
    var selectedOptionIndex by remember { mutableStateOf(-1) }
    var isAnswerCorrect by remember { mutableStateOf(false) }
    var showResult by remember { mutableStateOf(false) }

    if (currentQuestionIndex < questions.size) {
        val currentQuestion = questions[currentQuestionIndex]
        val options = currentQuestion.options.split(",")
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = currentQuestion.questionText,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(bottom = 16.dp)
            )
            options.forEachIndexed { index, option ->
                Button(
                    onClick = {
                        selectedOptionIndex = index
                        isAnswerCorrect = index == currentQuestion.correctAnswerIndex
                        showResult = true
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 8.dp)
                ) {
                    Text(text = option)
                }
            }
            if (showResult) {
                if (isAnswerCorrect) {
                    Text(text = "回答正确！", color = MaterialTheme.colorScheme.primary)
                } else {
                    Text(text = "回答错误，正确答案是：${options[currentQuestion.correctAnswerIndex]}", color = MaterialTheme.colorScheme.error)
                }
                Button(
                    onClick = {
                        currentQuestionIndex++
                        selectedOptionIndex = -1
                        showResult = false
                    },
                    modifier = Modifier.padding(top = 16.dp)
                ) {
                    Text(text = "下一题")
                }
            }
        }
    } else {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(text = "题目已全部完成！", fontWeight = FontWeight.Bold)
        }
    }
}